using JQData.Application.DataFetch;
using JQData.Core.DataModel;
using JQData.Core.DbModel;
using JQData.Core.TushareSDK.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace JQData.Application.Apis
{
    public class HotMoneyApi(ILogger<HotMoneyApi> _log,
        IRepository<HotMoney> _hmRepository,
        IRepository<HotMoneyStockCycleProfit> _hmscpDb,
        HotMoneyDataFetch _hotMoneyData) : IDynamicApiController
    {


        [Route("GetHotMoneyData")]
        public async Task<HotMoneyData> GetHotMoneyData(int hmid)
        {
            var data = await _hotMoneyData.GetHotMoneyData(hmid);
            return data;
        }

        [Route("GetHotMoneyList")]
        public async Task<PagedList<HotMoneyListItem>> GetHostMoneyList(int pageIndex = 1)
        {
            var dbList = await _hmRepository.Entities.ToPagedListAsync(pageIndex, 20);
            var items = new List<HotMoneyListItem>();
            var pageList = new PagedList<HotMoneyListItem>()
            {
                HasPrevPages = dbList.HasPrevPages,
                HasNextPages = dbList.HasNextPages,
                TotalCount = dbList.TotalCount,
                PageIndex = dbList.PageIndex,
                PageSize = dbList.PageSize,
                TotalPages = dbList.TotalPages,
                Items = items
            };


            foreach (var item in dbList.Items)
            {
                var itemData = await _hotMoneyData.GetHotMoneyData(item.Id);

                // 计算胜率：以当前仍有持仓的股票为基数，胜率=浮盈(stock.IsWin)的数量/持仓股票总数
                //var held = itemData.Day30.Where(i => i.RemainingPosition > 0);
                var totalHeld = itemData.Day30.Count;
                var winHeld = itemData.Day30.Count(i => i.IsWin);
                var winRate = totalHeld > 0 ? (float)Math.Round(winHeld / (double)totalHeld * 100, 2) : 0f;
                var dayStatsList = itemData.Day30.SelectMany(i => i.StockDayStats);

                var li = new HotMoneyListItem
                {
                    HotMoneyId = item.Id,
                    HotMoneyName = item.Name,
                    HeldStockCount = itemData.Day30.Count(i => i.RemainingPosition > 0),
                    NetBuyAmount = itemData.Day30.Where(i => i.NetBuyAmount > 0).Sum(i => i.NetBuyAmount),
                    WinPrice = dayStatsList.Sum(i => i.WinPrice),
                    BuyAmount = itemData.Day30.Sum(i => i.BuyAmount),
                    SellAmount = itemData.Day30.Sum(i => i.SellAmount),
                    WinRate = winRate
                };
                items.Add(li);
            }

            return pageList;
        }

        public class HotMoneyListItem
        {
            public int HotMoneyId { get; set; }

            public string HotMoneyName { get; set; }

            /// <summary>
            ///  持仓股票数
            /// </summary>
            public int HeldStockCount { get; set; }


            /// <summary>
            /// 买入总额
            /// </summary>
            public decimal BuyAmount { get; set; }

            /// <summary>
            /// 卖出总额
            /// </summary>
            public decimal SellAmount { get; set; }

            /// <summary>
            /// 持仓总额
            /// </summary>
            public decimal NetBuyAmount { get; set; }

            /// <summary>
            /// 盈利金额
            /// </summary>
            public decimal WinPrice { get; set; }

            /// <summary>
            /// 胜率
            /// </summary>
            public float WinRate { get; set; }
        }





        public class GetHotMoneyInfoResponse
        {
            public int HotMoneyId { get; set; }

            public string Name { get; set; }

            public string Desc { get; set; }

            public string Orgs { get; set; }


            public List<GetHotMoneyInfoChart> Charts { get; set; }

            public List<HotMoneyStockCycleProfit> StockCycleProfits { get; set; }
        }


        public class GetHotMoneyInfoChart
        {
            public string Title { get; set; }

            public string Value { get; set; }

            public string Chang { get; set; }

            public bool IsUp { get; set; }

            public List<string> Data { get; set; } = new List<string>();

            public List<string> Date { get; set; } = new List<string>();

            public string Type { get; set; }
        }


        public class GetHotMoneyInfoRequest
        {
            public string Name { get; set; }


        }

        /// <summary>
        /// 游资股票表格
        /// </summary>
        public class GetHotMoneyStockTableResponse
        {
            public int StockId { get; set; }

            public string StockName { get; set; }


            public List<GetHotMoneyStockTableItem> BuyInfo { get; set; }

            public List<GetHotMoneyStockTableItem> SellInfo { get; set; }

            public List<GetHotMoneyStockTableChartItem> Chart { get; set; }



            /// <summary>
            /// 仓位占比
            /// </summary>
            public float PositionRatio { get; set; }

            /// <summary>
            /// 成本
            /// </summary>
            public decimal CostPrice { get; set; }

            /// <summary>
            /// 剩余仓位
            /// </summary>
            public decimal RemainingPosition { get; set; }


            /// <summary>
            /// 是否盈利
            /// </summary>
            public bool? IsWin { get; set; }

            public DateOnly BuyDate { get; set; }


            /// <summary>
            /// 股票现价
            /// </summary>
            public decimal CurrentPrice { get; set; }

            public decimal BuyAmount => BuyInfo.Sum(i => i.Amount);
            public decimal SellAmount => SellInfo.Sum(i => i.Amount);
            public decimal NetBuyAmount => BuyAmount - SellAmount;

            public List<DailyResult> MarketData { get; set; }



        }

        public class GetHotMoneyStockTableItem
        {

            public decimal Amount { get; set; }

            public decimal Vol { get; set; }

            public decimal Price { get; set; }

        }


        /// <summary>
        /// 游资持仓情况表格内的图表数据
        /// </summary>
        public class GetHotMoneyStockTableChartItem
        {
            public string Date { get; set; }
            public decimal BuyVol { get; set; }

            public decimal SellVol { get; set; }
            public decimal Price { get; set; }
        }
    }
}
